<?php
	/**
 	 * Application model
 	*/
	class Model extends GlobalModel
	{
		/**
 		* Updates or insert a record
 		* @param array $data array of object data 
 		* @param string $stored_procedure stored procedure name (default: null)
 		* @return resource Result resource identifier
 		*/
		function Update($data, $stored_procedure = null, &$out_parameters = null)
		{
			global $object, $current_controller;
			
			//executes Update from global_model
			$update = parent::Update($data, $stored_procedure, &$out_parameters);
			
			//if $use_logging in config.php is set to TRUE and the default database is used
			if (($update)&&($object->config->use_logging)&&($this->database_config == 'db_config_default')&&(!empty($data)))
			{
				//loops through the data
				foreach ($data as $key=>$value)
				{
					//checks if the field is in db_structure
					if (isset($this->db_structure[$key])&&($current_controller->data[$current_controller->name][$key]!=$value))
					{
						//replaces ids with text (e.g. for combo boxes)
						if (isset($data[$key.'_text'])) $value = $data[$key.'_text'];
						
						//TODO: to save old value
//						$key_old = ereg_replace("(_id)$", "_name", $key);
//						$value_old = $current_controller->data[$current_controller->name][$key_old];
						
						//sets the old value
						//$value_old = '('.LANG_RECORD_ID.': '.$current_controller->data[$current_controller->name][$key].')';
						$value_old = $current_controller->data[$current_controller->name][$key];
						
						//sets the table name
						if (isset($this->db_structure[$key][table_name]))
						{
							$table_name = $this->db_structure[$key][table_name];
						}
						else
						{
							$table_name = $this->table_name;
						}
						
						//sets row_id (if it exists) and sets the action name
						if (isset($data[id]))
						{
							$row_id = $data[id];
							$action_name = 'update';
						}
						else
						{
							$row_id = '';
							$action_name = 'insert';
						}						
						
						//logs changes
						$current_controller->GetModelObject('systemLogs')->Execute
						(
							array
							(
								'action_name'=>$action_name,
								'user_id'=>$_SESSION[userid],
								'table_name'=>$table_name,
								'column_name'=>$this->db_structure[$key][name],
								'row_id'=>$row_id,
								'value_old'=>$value_old,
								'value'=>$value,
								'comment'=>$data['logcomment_'.$key]
							),
							'sp_system_logs_insert'
						);
					}
				}
			}
			
			return $update;
		}
		
	    /**
		 * Deletes the record
		 *
		 * @param array $parameters parameters string 
		 * @param string $stored_procedure stored procedure name (default: null)
  		*/			
		function Delete($parameters, $stored_procedure = null)
		{	
			global $object, $current_controller;

			//if $use_logging in config.php is set to TRUE and the default database is used
			if (($object->config->use_logging)&&($this->database_config == 'db_config_default'))
			{
				$key = $parameters;
	
				//sets the table name
				if (isset($this->db_structure[$key][table_name]))
				{
					$table_name = $this->db_structure[$key][table_name];
				}
				else
				{
					$table_name = $this->table_name;
				}
				
				$current_controller->GetModelObject('systemLogs')->Execute
				(array
					(
						'action_name'=>'delete',
						'user_id'=>$_SESSION[userid],
						'table_name'=>$table_name,
						'row_id'=>$key
					),
					'sp_system_logs_insert'
				);
			}
				
			//executes Delete from global_model
			parent::Delete($parameters, $stored_procedure = null);
		}
	}
?>